package com.herench.bunch.emr.convert;

import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v24.datatype.CE;
import ca.uhn.hl7v2.model.v24.group.RTB_K13_ROW_DEFINITION;
import ca.uhn.hl7v2.model.v24.message.QBP_Q11;
import ca.uhn.hl7v2.model.v24.message.RTB_K13;
import ca.uhn.hl7v2.model.v24.segment.MSH;
import ca.uhn.hl7v2.model.v24.segment.QPD;
import ca.uhn.hl7v2.model.v24.segment.RDT;
import ca.uhn.hl7v2.parser.DefaultModelClassFactory;
import com.herench.bunch.commons.support.RequestMessage;
import com.herench.bunch.commons.utils.IdGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;


public class DeptDictConvert extends AbstractMessageConvert {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public Map<String,Object> doToMessage(RequestMessage requestMessage) throws Exception {
        return null;
    }

    @SuppressWarnings("unchecked")
    public Message fromMessage(Object message) throws Exception {
        Assert.notNull(message, "message 为空.");
        List<Map<String, Object>> res = (List<Map<String, Object>> ) message;
        RTB_K13 rtbK13 = new RTB_K13();
        rtbK13.initQuickstart("RTB_K13", "RTB_K13", "P");
        MSH msh = rtbK13.getMSH();
        initResponseMessageMSH(msh);

        RTB_K13_ROW_DEFINITION definition = rtbK13.getROW_DEFINITION();
        int index = 0;
        for (Map<String,Object> resMap : res) {
            RDT rdt = new RDT(rtbK13, new DefaultModelClassFactory());
            CE ce = new CE(rtbK13);
            ce.getCe1_Identifier().setValue("RD");
            StringBuffer buffer = new StringBuffer();
            buffer.append("serialNo=").append(resMap.get("serialNo")).append("#")
                    .append("deptCode=").append(resMap.get("deptCode")).append("#")
                    .append("deptName=").append(resMap.get("deptName")).append("#")
                    .append("deptAlias=").append(resMap.get("deptAlias")).append("#")
                    .append("clinicAttr=").append(resMap.get("clinicAttr")).append("#")
                    .append("outpOrInp=").append(resMap.get("outpOrInp")).append("#")
                    .append("internalOrSergery=").append(resMap.get("internalOrSergery")).append("#")
                    .append("isGraveDept=").append(resMap.get("isGraveDept")).append("#")
                    .append("deptConsult=").append(resMap.get("deptConsult")).append("#")
                    .append("deptType=").append(resMap.get("deptType")).append("#")
                    .append("inputCode=").append(resMap.get("inputCode")).append("#")
                    .append("pym=").append(resMap.get("pym")).append("#")
                    .append("wbm=").append(resMap.get("wbm")).append("#")
                    .append("typeCode=").append(resMap.get("typeCode")).append("#")
                    .append("location=").append(resMap.get("location")).append("#")
                    .append("displayOrder=").append(resMap.get("displayOrder")).append("#")
                    .append("totalBedNum=").append(resMap.get("totalBedNum"));
            ce.getCe2_Text().setValue(buffer.toString());
            rdt.getColumnValue().setData(ce);
            definition.insertRDT(rdt, index++);
        }
        return rtbK13;
    }

    @Override
    public Message generatorReqMessage() throws Exception {
        QBP_Q11 qbp = new QBP_Q11();
        qbp.initQuickstart("QBP_Q11", "Q11", "L");
        MSH msh = qbp.getMSH();
        msh.getMsh3_SendingApplication().getHd1_NamespaceID().setValue("SSM");
        msh.getMessageControlID().setValue(IdGenerator.getUUID());
        msh.getMsh11_ProcessingID().getPt1_ProcessingID().setValue("200140");
        msh.getMsh11_ProcessingID().getProcessingMode().setValue("QR");
        QPD qpd = qbp.getQPD();
        qpd.getMessageQueryName().getIdentifier().setValue("查询号别");
        qpd.getQpd2_QueryTag().setValue("QR");
        return qbp;
    }
}
